package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.information.CreateInfoBo;
import com.zhuss.bean.bo.team.information.CreateInfoSonBo;
import com.zhuss.bean.bo.team.information.ViewInfoSonBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.*;
import com.zhuss.exception.WpsException;
import com.zhuss.team.db.dao.ext.InformationDao;
import com.zhuss.team.db.entity.Information;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/3 14:32
 */
@Component("informationCreate")
public class InformationCreate extends AbstractProcessor<Object, CreateInfoBo> implements Processor<Object, CreateInfoBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
    private UserRead userRead;

    @Autowired
    private InformationSonCreate informationSonCreate;

    @Override
    public Object process(CreateInfoBo bo) throws WpsException {
        Information info = new Information();
        info.setType(bo.getType());
        info.setCompanyId(bo.getCompanyId());
        info.setPerson(bo.getPerson());
        info.setTitle(bo.getTitle());
        info.setVisible(bo.getVisible());
        info.setContent(bo.getContent());
        info.setFormat(bo.getFormat());
        info.setArea(bo.getArea());
        info.setInfoKey(bo.getInfoKey());
        info.setHeat(0);

        info.setCreator(bo.getConcurrentUserName());
        info.setCreatorId(bo.getConcurrentUserId());
        info.setCreateTime(new Date());
        informationDao.create(info);

        CreateInfoSonBo infoSon = new CreateInfoSonBo();
        BeanUtils.copyProperties(bo, infoSon);
        List<Long> longList = new ArrayList<>();

        if (InfoVisible.访客.sameKey(info.getVisible())){
            return null;
        }else {
            if (bo.getSendType() == SendType.全部.key()){
                Object obj = userRead.listUserId(info.getArea());
                for (Object o : (List<Long>) obj) {
                    longList.add(Long.class.cast(o));
                }
                infoSon.setUserIdList(longList);
            }else if (bo.getSendType() == SendType.人员.key()){
                String[] strs = info.getPerson().split(",");
                for (String s : strs) {
                    longList.add(Long.parseLong(s));
                }
                infoSon.setUserIdList(longList);
            }else if (bo.getSendType() == SendType.单位.key()){
                List<Long> companyIdList = new ArrayList<>();
                String[] strs = info.getCompanyId().split(",");
                for (String s : strs) {
                    companyIdList.add(Long.parseLong(s));
                }
                Object obj = userRead.listUserIdByCompany(companyIdList);
                for (Object o : (List<Long>) obj) {
                    longList.add(Long.class.cast(o));
                }
                infoSon.setUserIdList(longList);
            }

            infoSon.setInfoId(info.getInfoId());
            infoSon.setInfoKey(info.getInfoKey());
            infoSon.setInfoTitle(info.getTitle());
            infoSon.setInfoType(info.getType());
            infoSon.setStatus(InfoSonStatus.待阅读.key());
            informationSonCreate.process(infoSon);
        }
        return null;
    }

    @Override
    public Object log(CreateInfoBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.信息发布创建.key());
        record.setTargetId(param.getTargetId());
        record.setTargetName(param.getTitle());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(CreateInfoBo createInfoBo) throws WpsException {

    }
}
